home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / screen32.lha / screen-3.2b / help.c < prev    next >
C/C++ Source or Header  |  1992-02-04  |  13KB  |  520 lines

  1. /* Copyright (c) 1991
  2.  *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
  3.  *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
  4.  * Copyright (c) 1987 Oliver Laumann
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program (see the file COPYING); if not, write to the
  18.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * Noteworthy contributors to screen's design and implementation:
  21.  *    Wayne Davison (davison@borland.com)
  22.  *    Patrick Wolfe (pat@kai.com, kailand!pat)
  23.  *    Bart Schaefer (schaefer@cse.ogi.edu)
  24.  *    Nathan Glasser (nathan@brokaw.lcs.mit.edu)
  25.  *    Larry W. Virden (lwv27%cas.BITNET@CUNYVM.CUNY.Edu)
  26.  *    Howard Chu (hyc@hanauma.jpl.nasa.gov)
  27.  *    Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
  28.  *    Markku Jarvinen (mta@{cc,cs,ee}.tut.fi)
  29.  *    Marc Boucher (marc@CAM.ORG)
  30.  *
  31.  ****************************************************************
  32.  */
  33.  
  34. #ifndef lint
  35.   static char rcs_id[] = "$Id: help.c,v 1.3 92/02/04 21:38:01 mlschroe Exp $ FAU";
  36. #endif
  37.  
  38. #include "config.h"
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41.  
  42. #ifdef BSDI
  43. # include <sys/signal.h>
  44. #endif /* BSDI */
  45.  
  46. #include "screen.h"
  47. #include "ansi.h"
  48. #include "extern.h"
  49. #include "patchlevel.h"
  50.  
  51. int help_page = 0;
  52. int command_search, command_bindings = 0;
  53. extern char Esc, MetaEsc;
  54. extern char *KeyNames[];
  55. extern struct key ktab[];
  56. extern int screenwidth, screenheight;
  57. extern char *blank, *null, *CE;
  58. extern struct win *fore;
  59.  
  60. static void centerline __P((char *));
  61. static void HelpRedisplayLine __P((int, int, int, int));
  62. static void process_help_input __P((char **, int *));
  63. static void AbortHelp __P((void));
  64. static void add_key_to_buf __P((char *, int));
  65.  
  66. void
  67. exit_with_usage(myname)
  68. char *myname;
  69. {
  70.   printf("Use: %s [-opts] [cmd [args]]\n", myname);
  71.   printf(" or: %s -r [host.tty]\n\nOptions:\n", myname);
  72.   printf("-a           Force all capabilities into each window's termcap\n");
  73.   printf("-A -[r|R]    Adapt all windows to the new display width & height\n");
  74.   printf("-c file      Read configuration file instead of .screenrc\n");
  75. #ifdef REMOTE_DETACH
  76.   printf("-d (-r)      Detach the elsewhere running screen (and reattach here)\n");
  77.   printf("-D (-r)      Detach and logout remote (and reattach here)\n");
  78. #endif
  79.   printf("-e xy        Change command characters\n");
  80.   printf("-f           Flow control on, -fn = off, -fa = auto\n");
  81.   printf("-h lines     Set the size of the scrollback history buffer\n");
  82.   printf("-i           Interrupt output sooner when flow control is on\n");
  83.   printf("-l           Login mode on (update %s), -ln = off\n", UTMPFILE);
  84.   printf("-list        or -ls. Do nothing, just list our SockDir\n");
  85.   printf("-L           Terminal's last character can be safely updated\n");
  86.   printf("-O           Choose optimal output rather than exact vt100 emulation\n");
  87.   printf("-q           Quiet startup. Sets $status if unsuccessful.\n");
  88.   printf("-r           Reattach to a detached screen process\n");
  89.   printf("-R           Reattach if possible, otherwise start a new session\n");
  90.   printf("-s shell     Shell to execute rather than $SHELL\n");
  91.   printf("-T term      Use term as $TERM for windows, rather than \"screen\"\n");
  92.   printf("-t title     Set command's a.k.a. (window title)\n");
  93.   printf("-wipe        Do nothing, just clean up SockDir\n");
  94.   exit(1);
  95. }
  96.  
  97. /* Esc-char is not consumed. All others are. Esc-char, space, and return end */
  98. static void
  99. process_help_input(ppbuf, plen)
  100. char **ppbuf;
  101. int *plen;
  102. {
  103.   int done = 0;
  104.  
  105.   if (ppbuf == 0)
  106.     {
  107.       AbortHelp();
  108.       return;
  109.     }
  110.   while (!done && *plen > 0)
  111.     {
  112.       switch (**ppbuf)
  113.     {
  114.     case ' ':
  115.       if (display_help() == 0)
  116.             break;
  117.       /* FALLTHROUGH */
  118.     case '\r':
  119.     case '\n':
  120.       done = 1;
  121.       break;
  122.     default:
  123.       if (**ppbuf == Esc)
  124.         {
  125.           done = 1;
  126.           continue;
  127.         }
  128.       break;
  129.     }
  130.       ++*ppbuf;
  131.       --*plen;
  132.     }
  133.   if (done)
  134.     AbortHelp();
  135. }
  136.  
  137. static void
  138. AbortHelp()
  139. {
  140.   help_page = 0;
  141.   ExitOverlayPage();
  142.   Activate(0);
  143. }
  144.  
  145. static int maxrow, grow, numcols, numrows, num_names;
  146. static int numskip, numpages;
  147.  
  148. int
  149. display_help()
  150. {
  151.   int col, crow, n, key = 0;
  152.   enum keytype typ = ktab[0].type;
  153.   char buf[256], Esc_buf[5], cbuf[256];
  154.  
  155.   if (!help_page++)
  156.     {
  157.       if (screenwidth < 26 || screenheight < 6)
  158.         {
  159.       Msg(0, "Window size too small for help page");
  160.       help_page = 0;
  161.       return -1;
  162.         }
  163.       InitOverlayPage(process_help_input, HelpRedisplayLine, (int (*)())0, 0);
  164.  
  165.       command_bindings = 0;
  166.       for (key = 0; key < 256; key++)
  167.         if ((typ = ktab[key].type) == KEY_CREATE
  168.         || typ == KEY_SCREEN
  169.         || typ == KEY_SET
  170.         || (typ == KEY_AKA && ktab[key].args))
  171.       command_bindings++;
  172.       debug1("help: command_bindings counted: %d\n",command_bindings);
  173.       for (n = 0; KeyNames[n] != NULL; n++)
  174.     ; /* we dont know "sizeof * KeyNames" */
  175.       num_names = n - 1;
  176.       debug1("help: we find %d named keys (+1).\n", num_names);
  177.       command_search = 0;
  178.  
  179.       numcols = screenwidth/26;
  180.       if (numcols == 0)
  181.         numcols = 1;
  182.       numrows = (num_names + numcols -1) / numcols;
  183.       debug1("Numrows: %d\n", numrows);
  184.       numskip = screenheight-5 - (2 + numrows);
  185.       while (numskip < 0)
  186.     numskip += screenheight-5;
  187.       numskip %= screenheight-5;
  188.       debug1("Numskip: %d\n", numskip);
  189.       if (numskip > screenheight/3 || numskip > command_bindings)
  190.     numskip = 1;
  191.       maxrow = 2 + numrows + numskip + command_bindings;
  192.       grow = 0;
  193.  
  194.       numpages = (maxrow + screenheight-6) / (screenheight-5);
  195.     }
  196.  
  197.   if (grow >= maxrow)
  198.     { 
  199.       return(-1);
  200.     }
  201.  
  202.   /* Clear the help screen */
  203.   ClearDisplay();
  204.   
  205.   sprintf(cbuf,"Screen key bindings, page %d of %d.", help_page, numpages);
  206.   centerline(cbuf);
  207.   printf("\n");
  208.   crow = 2;
  209.  
  210.   *Esc_buf = '\0';
  211.   add_key_to_buf(Esc_buf, Esc);
  212.   Esc_buf[strlen(Esc_buf) - 1] = '\0';
  213.  
  214.   for (; crow < screenheight - 3; crow++)
  215.     {
  216.       if (grow < 1)
  217.         {
  218.          *buf = '\0';
  219.           add_key_to_buf(buf, MetaEsc);
  220.           buf[strlen(buf) - 1] = '\0';
  221.           sprintf(cbuf,"Command key:  %s   Literal %s:  %s", Esc_buf, Esc_buf, buf);
  222.           centerline(cbuf);
  223.       grow++;
  224.         }
  225.       else if (grow >= 2 && grow-2 < numrows)
  226.     {
  227.       for (col = 0; col < numcols && (n = numrows * col + (grow-2)) < num_names; col++)
  228.         {
  229.           debug1("help: searching key %d\n", n);
  230.           buf[0] = '\0';
  231.           for (key = 0; key < 128; key++)
  232.         if (ktab[key].type == (enum keytype) (n + 2)
  233.             && ((enum keytype) (n + 2) != KEY_AKA || !ktab[key].args) )
  234.           add_key_to_buf(buf, key);
  235.           buf[14] = '\0';
  236.           /*
  237.            * Format is up to 10 chars of name, 1 spaces, 14 chars of key
  238.            * bindings, and a space.
  239.            */
  240.           printf("%-10.10s %-14.14s ", KeyNames[n + 1], buf);
  241.         }
  242.       printf("\r\n");
  243.           grow++;
  244.         }
  245.       else if (grow-2-numrows >= numskip 
  246.                && grow-2-numrows-numskip < command_bindings)
  247.         {
  248.           char **pp, *cp;
  249.  
  250.       while (command_search < 128
  251.          && (typ = ktab[command_search].type) != KEY_CREATE
  252.          && typ != KEY_SCREEN
  253.          && typ != KEY_SET
  254.          && (typ != KEY_AKA || !ktab[command_search].args))
  255.         command_search++;
  256.       buf[0] = '\0';
  257.       add_key_to_buf(buf, command_search);
  258.       printf("%-4s", buf);
  259.       col = 4;
  260.       if (typ != KEY_CREATE)
  261.         {
  262.           col += strlen(KeyNames[(int)typ - 1]) + 1;
  263.           printf("%s ", KeyNames[(int)typ - 1]);
  264.         }
  265.       pp = ktab[command_search++].args;
  266.       while (pp && (cp = *pp) != NULL)
  267.         {
  268.           if (!*cp || (index(cp, ' ') != NULL))
  269.         {
  270.           if (index(cp, '\'') != NULL)
  271.             *buf = '"';
  272.           else
  273.             *buf = '\'';
  274.           sprintf(buf + 1, "%s%c", cp, *buf);
  275.           cp = buf;
  276.         }
  277.           if ((col += strlen(cp) + 1) >= screenwidth)
  278.         {
  279.           col = screenwidth - (col - (strlen(cp) + 1)) - 2;
  280.           if (col >= 0)
  281.             {
  282.               n = cp[col];
  283.               cp[col] = '\0';
  284.               printf("%s$", *pp);
  285.               cp[col] = (char) n;
  286.               }
  287.               break;
  288.             }
  289.           printf("%s%c", cp, (screenwidth - col != 1 || !pp[1]) ? ' ' : '$');
  290.           pp++;
  291.         }
  292.       printf("\r\n");
  293.       grow++;
  294.     }
  295.       else
  296.     {
  297.           putchar('\n');
  298.       grow++;
  299.     }
  300.     }
  301.   printf("\n");
  302.   sprintf(cbuf,"[Press Space %s Return to end; %s to begin a command.]",
  303.      grow < maxrow ? "for next page;" : "or", Esc_buf);
  304.   centerline(cbuf);
  305.   fflush(stdout);
  306.   SetLastPos(0, screenheight-1);
  307.   return(0);
  308. }
  309.  
  310. static void
  311. add_key_to_buf(buf, key)
  312. char *buf;
  313. int key;
  314. {
  315.   debug1("help: key found: %c\n", key);
  316.   switch (key)
  317.     {
  318.     case ' ':
  319.       strcat(buf, "sp ");
  320.       break;
  321.     case 0x7f:
  322.       strcat(buf, "^? ");
  323.       break;
  324.     default:
  325.       if (key < ' ')
  326.     sprintf(buf + strlen(buf), "^%c ", (key | 0x40));
  327.       else
  328.     sprintf(buf + strlen(buf), "%c ", key);
  329.       break;
  330.     }
  331. }
  332.  
  333. static void
  334. centerline(str)
  335. char *str;
  336. {
  337.   int l;
  338.   l = (screenwidth - 1 + strlen(str)) / 2;
  339.   if (l > screenwidth - 1)
  340.     l = screenwidth - 1;
  341.   printf("%*.*s\r\n", l, l, str);
  342. }
  343.  
  344. static void
  345. HelpRedisplayLine(y, xs, xe, isblank)
  346. int y, xs, xe, isblank;
  347. {
  348.   if (isblank)
  349.     return;
  350.   if (CE)
  351.     {
  352.       GotoPos(xs, y);
  353.       PutStr(CE);
  354.       return;
  355.     }
  356.   DisplayLine(null, null, null, blank, null, null, y, xs, xe);
  357. }
  358.  
  359. /*
  360.  * here all the copyright stuff 
  361.  */
  362.  
  363.  
  364. static char version[40];
  365.  
  366. static char cpmsg[] = "\
  367. \n\
  368. iScreen version %v\n\
  369. \n\
  370. Copyright (c) 1991\n\
  371.     Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)\n\
  372.     Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)\n\
  373. Copyright (c) 1987 Oliver Laumann\n\
  374. \n\
  375. This program is free software; you can redistribute it and/or \
  376. modify it under the terms of the GNU General Public License as published \
  377. by the Free Software Foundation; either version 1, or (at your option) \
  378. any later version.\n\
  379. \n\
  380. This program is distributed in the hope that it will be useful, \
  381. but WITHOUT ANY WARRANTY; without even the implied warranty of \
  382. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \
  383. GNU General Public License for more details.\n\
  384. \n\
  385. You should have received a copy of the GNU General Public License \
  386. along with this program (see the file COPYING); if not, write to the \
  387. Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n";
  388.  
  389.  
  390. static void process_copyright_input __P((char **, int *));
  391. static void AbortCopyright __P((void));
  392. static void copypage __P((void));
  393.  
  394. static char *cps, *savedcps;
  395.  
  396. static void
  397. process_copyright_input(ppbuf, plen)
  398. char **ppbuf;
  399. int *plen;
  400. {
  401.   int done = 0;
  402.  
  403.   if (ppbuf == 0)
  404.     {
  405.       AbortCopyright();
  406.       return;
  407.     }
  408.   while (!done && *plen > 0)
  409.     {
  410.       switch (**ppbuf)
  411.     {
  412.     case ' ':
  413.           if (*cps)
  414.         {
  415.           copypage();
  416.           break;
  417.         }
  418.       /* FALLTHROUGH */
  419.     case '\r':
  420.     case '\n':
  421.       AbortCopyright();
  422.       done = 1;
  423.       break;
  424.     default:
  425.       break;
  426.     }
  427.       ++*ppbuf;
  428.       --*plen;
  429.     }
  430. }
  431.  
  432. static void
  433. AbortCopyright()
  434. {
  435.   ExitOverlayPage();
  436.   Activate(0);
  437. }
  438.  
  439. void
  440. display_copyright()
  441. {
  442.   if (screenwidth < 10 || screenheight < 5)
  443.     {
  444.       Msg(0, "Window size too small for copyright page");
  445.       return;
  446.     }
  447.   InitOverlayPage(process_copyright_input, HelpRedisplayLine, (int (*)())0, 0);
  448.   sprintf(version, "%d.%.2d.%.2d%s (%s) %s", REV, VERS, PATCHLEVEL, STATE, ORIGIN, DATE);
  449.   cps = cpmsg;
  450.   savedcps = 0;
  451.   copypage();
  452. }
  453.  
  454.  
  455. static void
  456. copypage()
  457. {
  458.   char *ws;
  459.   int x, y, l;
  460.   char cbuf[80];
  461.  
  462.   ClearDisplay();
  463.   x = y = 0;
  464.   while(*cps)
  465.     {
  466.       ws = cps;
  467.       while (*cps == ' ')
  468.     cps++;
  469.       if (strncmp(cps, "%v", 2) == 0)
  470.     {
  471.       savedcps = cps + 2;
  472.       ws = cps = version;
  473.     }
  474.       while (*cps && *cps != ' ' && *cps != '\n')
  475.     cps++;
  476.       l = cps - ws;
  477.       cps = ws;
  478.       if (l > screenwidth - 1)
  479.     l = screenwidth - 1;
  480.       if (x && x + l >= screenwidth - 2)
  481.     {
  482.       printf("\r\n");
  483.       x = 0;
  484.       if (++y > screenheight - 4)
  485.             break;
  486.     }
  487.       if (x)
  488.     {
  489.       putchar(' ');
  490.       x++;
  491.     }
  492.       if (l)
  493.         printf("%*.*s", l, l, ws);
  494.       x += l;
  495.       cps += l;
  496.       if (*cps == 0 && savedcps)
  497.     {
  498.       cps = savedcps;
  499.       savedcps = 0;
  500.     }
  501.       if (*cps == '\n')
  502.     {
  503.       printf("\r\n");
  504.       x = 0;
  505.       if (++y > screenheight - 4)
  506.             break;
  507.     }
  508.       if (*cps == ' ' || *cps == '\n')
  509.     cps++;
  510.     }
  511.   while (y++ < screenheight - 2)
  512.     printf("\r\n");
  513.   sprintf(cbuf,"[Press Space %s Return to end.]",
  514.      *cps ? "for next page;" : "or");
  515.   centerline(cbuf);
  516.   fflush(stdout);
  517.   SetLastPos(0, screenheight-1);
  518. }
  519.   
  520.